//
//  DiscoverCollectionViewCellOfContentViewSecond.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/31.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "DiscoverCollectionViewCellOfContentViewSecond.h"
#import "XYMacro.h"

@implementation DiscoverCollectionViewCellOfContentViewSecond

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((SCREEN_WIDTH - 130) / 3, (SCREEN_WIDTH - 130) / 3);
        layout.minimumLineSpacing = 30;
        layout.minimumInteritemSpacing = 30;
        layout.sectionInset = UIEdgeInsetsMake(30, 35, 30, 35);
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = BACKGROUNDCOLOR;
        [self.contentView addSubview:_collectionView];
        [_collectionView registerClass:[DiscoverCollectionViewCellOfContentViewSixth class] forCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentViewSixth"];
        [self pullDownRefresh];
    }
    return self;
}


#pragma mark --- 刷新------
- (void)pullDownRefresh {
    MJRefreshGifHeader *header = [MJRefreshGifHeader  headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.collectionView.mj_header beginRefreshing];
    
    NSMutableArray *mArr = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"list_load_%d", i]];
        [mArr addObject:image];
    }
    
    [header setImages:mArr forState:MJRefreshStateRefreshing];
    header.stateLabel.hidden = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.collectionView.mj_header = header;
}

- (void)loadNewData {
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/channellist.jhtml" andBody:@"json=%7B%7D"andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        DiscoverModelSecond *model = [DiscoverModelSecond modelWithDictionary:result];
        if (model.status.succeed == 1) {
            _model = model;
            [self.collectionView reloadData];
            [self.collectionView.mj_header endRefreshing];
        }
    } andFailureBlock:^(NSError *error) {}];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _model.data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DiscoverCollectionViewCellOfContentViewSixth *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentViewSixth" forIndexPath:indexPath];
    cell.model= [_model.data objectAtIndex:indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    VCOfClassify *nextClassifyVC = [[VCOfClassify alloc] init];
    VCOfDiscover *discoverVC = (VCOfDiscover *)self.nextResponder.nextResponder.nextResponder;
    DiscoverModelSecondContant *model = [_model.data objectAtIndex:indexPath.item];
    nextClassifyVC.channel_id = [NSString stringWithFormat:@"%ld", model.channel_id];
    nextClassifyVC.title = model.channel_name;
    [discoverVC.navigationController pushViewController:nextClassifyVC animated:YES];
    
}


- (void)setModel:(DiscoverModelSecond *)model {
    
    _model = [[DiscoverModelSecond alloc] init];
    _model = model;
    
}

@end
